
<template>
    <el-color-picker
            v-model="theme"
            :predefine="['#3A8DD2', '#1989fa', '#5cb87a','#e6a23c','#f56c6c','#7BAA57','#3C90AE','#5290E8','#5D5FE7','#9460ED','#BE5BD7','#D75B88','#AA5757','#57AAA7','#4972AA','#4954AA','#5949AA','#8B50CF','#A950CF','#CF5092']"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
    />
</template>

<script>
    import string from "@/utils/style.js"
//   默认主题     iv:#2d8cf0     el:#409EFF       co :  #321fdb
    export default {
        data() {
            return {
                originalCluster: [],
                chalk: string.stringData, // content of theme-chalk css
                theme: this.defaultColor
            }
        },
        props:{
            defaultColor:{
                type: String,
                default: ""
            }


        },
        computed: {
            defaultTheme() {
                return this.$store.state.colorPick
            }
        },

        watch: {
            defaultTheme: {
                handler: function(val, oldVal) {
                    this.theme = val
                },
                immediate: true
            },
             theme(val) {
                 this.changeColorThem(val);
            }
        },
        mounted(){
            this.getOldColor();
            this.changeColorThem(this.theme);
        },
        methods: {
            changeColorThem(val){
                if (typeof val !== 'string') return
                // 得到新的对应的颜色值
                const getHandler = (variable, id) => {
                    return () => {
                        let newColor = [];
                        const themeCluster = this.getThemeCluster(val.replace('#', ''));
                        newColor=themeCluster.concat(themeCluster).concat(themeCluster)
                        const newStyle = this.updateStyle(this[variable], this.originalCluster, newColor)
                        let styleTag = document.getElementById(id)
                        if (!styleTag) {
                            styleTag = document.createElement('style')
                            styleTag.setAttribute('id', id)
                            document.head.appendChild(styleTag)
                        }
                        styleTag.innerText = newStyle
                    }
                }
                const chalkHandler = getHandler('chalk', 'chalk-style')
                chalkHandler()
                this.$emit('onThemeChange', val)
              /*  // 获取所有的style 合计，并且一次替换对应的颜色值，自己另写的颜色值不算
                const styles = [].slice.call(document.querySelectorAll('style')).filter( style => {
                    const text = style.innerText
                    return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
                })
                styles.forEach(style => {
                    const { innerText } = style
                    if (typeof innerText !== 'string') return
                    style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
                })
*/
                // 向外抛出数据 并存进vuex中

            },
            getOldColor() {
                let originalCluster = [];
                let defaultTheme = ["#2d8cf0", "#409EFF", "#321fdb" ];
                for (let i=0;i<defaultTheme.length;i++){
                    let oldColor=this.getThemeCluster(defaultTheme[i].replace('#', ''));
                    originalCluster=originalCluster.concat(oldColor)
                }
                this.originalCluster = originalCluster;
            },
            //  根据旧值替换样式字符串中的旧值  this[variable], originalCluster, themeCluster
            updateStyle(style, oldCluster, newCluster) {
                let newStyle = style
                oldCluster.forEach((color, index) => {
                    newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
                })
                return newStyle
            },
            // 生成颜色值
            getThemeCluster(theme) {
                const tintColor = (color, tint) => {
                    let red = parseInt(color.slice(0, 2), 16)
                    let green = parseInt(color.slice(2, 4), 16)
                    let blue = parseInt(color.slice(4, 6), 16)

                    if (tint === 0) { // when primary color is in its rgb space
                        return [red, green, blue].join(',')
                    } else {
                        red += Math.round(tint * (255 - red))
                        green += Math.round(tint * (255 - green))
                        blue += Math.round(tint * (255 - blue))

                        red = red.toString(16)
                        green = green.toString(16)
                        blue = blue.toString(16)

                        return `#${red}${green}${blue}`
                    }
                }

                const shadeColor = (color, shade) => {
                    let red = parseInt(color.slice(0, 2), 16)
                    let green = parseInt(color.slice(2, 4), 16)
                    let blue = parseInt(color.slice(4, 6), 16)

                    red = Math.round((1 - shade) * red)
                    green = Math.round((1 - shade) * green)
                    blue = Math.round((1 - shade) * blue)

                    red = red.toString(16)
                    green = green.toString(16)
                    blue = blue.toString(16)

                    return `#${red}${green}${blue}`
                }

                const clusters = [theme]
                for (let i = 0; i <= 9; i++) {
                    clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
                }
                clusters.push(shadeColor(theme, 0.1))
                return clusters
            },
        }
    }
</script>

<style>
    .theme-message,
    .theme-picker-dropdown {
        z-index: 99999 !important;
    }

    .theme-picker .el-color-picker__trigger {
        height: 26px !important;
        width: 26px !important;
        padding: 2px;
    }

    .theme-picker-dropdown .el-color-dropdown__link-btn {
        display: none;
        color: #321fdb;
    }
</style>

